﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.entity;
using BOM.dao;
using BOM.PriceBill;

namespace BOM.BaseData
{
    public partial class Customer : Form
    {
        public Customer()
        {
            InitializeComponent();
            Customer0 customer = new Customer0();
        }
        //新增按钮
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            txtNumber.ReadOnly = false;
            txtCustomerName.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtEmail.ReadOnly = false;
            txtLinkman.ReadOnly = false;
            txtMobile.ReadOnly = false;
            txtQQ.ReadOnly = false;
            txtTel.ReadOnly = false;
            txtNumber.Text = "";
            txtCustomerName.Text = "";
            txtLinkman.Text = "";
            txtAddress.Text = "";
            txtMobile.Text = "";
            txtTel.Text = "";
            txtEmail.Text = "";
            txtQQ.Text="";
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
        }
        //修改按钮 作用是解除文本框不可输入的状态
        private void btnEdit_Click(object sender, EventArgs e)
        {
            txtNumber.ReadOnly = true;
            txtCustomerName.ReadOnly = false;
            txtAddress.ReadOnly = false;
            txtEmail.ReadOnly = false;
            txtLinkman.ReadOnly = false;
            txtMobile.ReadOnly = false;
            txtQQ.ReadOnly = false;
            txtTel.ReadOnly = false;
            btnSave.Enabled = true;
        }
        //删除按钮
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //提示用户是否删除
            DialogResult dr = MessageBox.Show("您确定要删除吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                CustomerNew ShanChu = new CustomerNew();
                int i = ShanChu.CustomerShanChu();
                if (i == 0)
                    MessageBox.Show("删除失败", "提示");
                else
                {
                    CustomerList culist = (CustomerList)this.Owner;
                    culist.BinData();
                    MessageBox.Show("删除成功", "提示");
                }
                txtNumber.Text = "";
                txtCustomerName.Text = "";
                txtLinkman.Text = "";
                txtAddress.Text = "";
                txtMobile.Text = "";
                txtTel.Text = "";
                txtEmail.Text = "";
                txtQQ.Text = "";
            }
        }
        //保存按钮
        private void btnSave_Click(object sender, EventArgs e)
        {
            Customer0 customer = new Customer0();
            customer.setFnumber(txtNumber.Text);
            customer.setFname(txtCustomerName.Text);
            customer.setFlinkman(txtLinkman.Text);
            customer.setFaddress(txtAddress.Text);
            customer.setFobile(txtMobile.Text);
            customer.setFtel(txtTel.Text);
            customer.setFemail(txtEmail.Text);
            customer.setFqq(txtQQ.Text);
            if (txtCustomerName.Text.Trim() == "" || txtNumber.Text.Trim() == "" ||(txtMobile.Text.Trim()=="" &&　txtTel.Text.Trim()==""))
            {
                MessageBox.Show("编号或姓名或联系方式不能为空", "提示");
            }
            else
            {
                CustomerNew XiuGai = new CustomerNew();
                if (XiuGai.CustomerChaXun() == 1)
                {
                    int i = XiuGai.CustomerXiuGai();
                    if (i == 1)
                    {
                        DialogResult dr = MessageBox.Show("该客户已存在您确定要修改吗？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (dr == DialogResult.OK)
                        {
                            CustomerList culist = (CustomerList)this.Owner;
                            culist.BinData();
                            MessageBox.Show("修改成功", "提示");
                        }
                        else
                        {
                            MessageBox.Show("修改失败", "提示");
                        }
                    }
                }
                if (XiuGai.CustomerChaXun() == 0)
                {
                    int i = XiuGai.CustomerAdd();
                    if (i == 1)
                    {
                        CustomerList culist = (CustomerList)this.Owner;
                        culist.BinData();
                        MessageBox.Show("新增成功", "提示");
                    }
                    else
                    {
                        MessageBox.Show("新增失败", "提示");
                    }
                }
            }
        }
        //加载事件
        private void Customer_Load(object sender, EventArgs e)
        {
            if (PriceBillForm.number == 1)
            {
                try
                {
                    DataTable table = CustomerNew.CustomerNumberSelect(PriceBillForm.fname);
                    if (table.Rows.Count == 1)
                    {
                        txtNumber.Text = table.Rows[0]["fnumber"].ToString();
                        txtCustomerName.Text = table.Rows[0]["fname"].ToString();
                        txtLinkman.Text = table.Rows[0]["flinkman"].ToString();
                        txtAddress.Text = table.Rows[0]["faddress"].ToString();
                        txtMobile.Text = table.Rows[0]["fmobile"].ToString();
                        txtTel.Text = table.Rows[0]["ftel"].ToString();
                        txtEmail.Text = table.Rows[0]["femail"].ToString();
                        txtQQ.Text = table.Rows[0]["fqq"].ToString();
                        txtNumber.ReadOnly = true;
                        txtAddress.ReadOnly = true;
                        txtEmail.ReadOnly = true;
                        txtLinkman.ReadOnly = true;
                        txtMobile.ReadOnly = true;
                        txtQQ.ReadOnly = true;
                        txtTel.ReadOnly = true;
                        txtCustomerName.ReadOnly = true;
                        btnAddNew.Enabled = false;
                        btnSave.Enabled = false;
                        btnDelete.Enabled = false;
                        btnEdit.Enabled = false;
                    }
                }
                catch
                { }
            }
            else
            {
                if (CustomerList.BiaoJi == 2) //2 代表新增
                {
                    txtNumber.Text = "";
                    txtCustomerName.Text = "";
                    txtLinkman.Text = "";
                    txtAddress.Text = "";
                    txtMobile.Text = "";
                    txtTel.Text = "";
                    txtEmail.Text = "";
                    txtQQ.Text = "";
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    CustomerList.BiaoJi = -1; //标记赋值为 -1 待用
                }
                else if (CustomerList.BiaoJi == 1) //修改
                {
                    try
                    {
                        CustomerNew customer = new CustomerNew();
                        int i = customer.CustomerChak(CustomerList.XuanZID);
                        if (i == 1)
                        {
                            Customer0 cutr = new Customer0();
                            txtNumber.Text = cutr.getFnumber();
                            txtNumber.ReadOnly = true;
                            txtCustomerName.Text = cutr.getFname();
                            txtLinkman.Text = cutr.getFlinkman();
                            txtAddress.Text = cutr.getFaddress();
                            txtMobile.Text = cutr.getFobile();
                            txtTel.Text = cutr.getFtel();
                            txtEmail.Text = cutr.getFemail();
                            txtQQ.Text = cutr.getFqq();
                        }
                        CustomerList.BiaoJi = -1;
                    }
                    catch
                    { }
                }
                else if (CustomerList.BiaoJi == 0) //查看
                {
                    btnSave.Enabled = false;
                    try
                    {
                        CustomerNew customer = new CustomerNew();
                        int i = customer.CustomerChak(CustomerList.XuanZID);
                        if (i == 1)
                        {
                            Customer0 cutr = new Customer0();
                            txtNumber.Text = cutr.getFnumber();
                            txtCustomerName.Text = cutr.getFname();
                            txtLinkman.Text = cutr.getFlinkman();
                            txtAddress.Text = cutr.getFaddress();
                            txtMobile.Text = cutr.getFobile();
                            txtTel.Text = cutr.getFtel();
                            txtEmail.Text = cutr.getFemail();
                            txtQQ.Text = cutr.getFqq();
                        }
                        txtNumber.ReadOnly = true;
                        txtAddress.ReadOnly = true;
                        txtEmail.ReadOnly = true;
                        txtLinkman.ReadOnly = true;
                        txtMobile.ReadOnly = true;
                        txtQQ.ReadOnly = true;
                        txtTel.ReadOnly = true;
                        txtCustomerName.ReadOnly = true;
                        CustomerList.BiaoJi = -1;
                    }
                    catch
                    { }
                }
            }
        }
        //退出按钮
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
